package com.yj.provide.controller;

import com.yj.pojo.Payment;
import com.yj.pojo.Result;
import com.yj.provide.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public Result create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        log.info(i + "条插入成功");
        if (i > 0) {
            return new Result(200, "插入成功" + serverPort, i);
        } else {
            return new Result(444, "插入失败" + serverPort);
        }
    }

    @GetMapping("/get/{id}")
    public Result queryById(@PathVariable("id") Long id) {
        Payment payment = paymentService.queryById(id);
        log.info("查询成功" + payment);
        if (payment != null) {
            return new Result(200, "查询成功" + serverPort, payment);
        } else {
            return new Result(444, "查询失败" + serverPort);
        }
    }

    @GetMapping("/timeout")
    public String paymentTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return serverPort;
    }
}
